<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/* md5md5 */
function mmd5($str, $short = false){
	$return = md5(substr(md5($str), 8, 16));
	if($short){
		$return = substr($return, 8, 16);
	}
	return $return;
}

/* 毫秒时间戳 */
function msectime(){
	list($msec, $sec) = explode(' ', microtime());
	$return = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
	return $return;
}

/* 加、解密字符 */
function secret_char($string, $encode = true, $key = 's*K8'){
	$key = md5($key);
	$key_length = strlen($key);
	$string = !$encode ? base64_decode($string) : substr(md5($string.$key), 0, 8).$string;
	$string_length = strlen($string);
	$rndkey = $box = array();
	$result = '';
	for($i = 0; $i <= 255; $i++) {
		$rndkey[$i] = ord($key[$i % $key_length]);
		$box[$i] = $i;
	}
	for($j = $i = 0; $i < 256; $i++) {
		$j = ($j + $box[$i] + $rndkey[$i]) % 256;
		$tmp = $box[$i];
		$box[$i] = $box[$j];
		$box[$j] = $tmp;
	}
	for($a = $j = $i = 0; $i < $string_length; $i++) {
		$a = ($a + 1) % 256;
		$j = ($j + $box[$a]) % 256;
		$tmp = $box[$a];
		$box[$a] = $box[$j];
		$box[$j] = $tmp;
		$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
	}
	if(!$encode){
		if(substr($result, 0, 8) == substr(md5(substr($result, 8).$key), 0, 8)){
			return substr($result, 8);
		}
		return '';
	}
	return str_replace('=', '', base64_encode($result));
}

/* 唯一编号 */
function unique_id(){
	return date('Ymd').sprintf('%05d', date('G')*3600 + date('i')*60 + date('s')).substr(microtime(), 2, 5).sprintf('%02d', rand(0, 99));
}